package com.kfm.base.oop3.book;

import java.util.Arrays;

public class Demo {


    public static void main(String[] args) {
        /*
          存 5 本书
          按出版年份排序
         */
        Book book1 = new Book("第1本书", "A", 2001, 49.8);
        Book book2 = new Book("第2本书", "A", 2011, 39.8);
        Book book3 = new Book("第3本书", "A", 2008, 29.8);
        Book book4 = new Book("第4本书", "A", 2002, 69.8);
        Book book5 = new Book("第5本书", "A", 2021, 99.8);

        Book[] arr = {book1, book2, book3, book4, book5};

        sort(arr);

        System.out.println(Arrays.toString(arr));
    }

    public static void sort(Book[] arr){

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j].getYear() > arr[j + 1].getYear()){
                    Book temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
